/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package source;

import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.security.spec.RSAKeyGenParameterSpec;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.joda.time.DateTime;
import org.bouncycastle.jce.X509Principal;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.x509.X509V3CertificateGenerator;

public class GeneradorCertificados {

       
        public GeneradorCertificados() throws Exception {
                Security.addProvider(new BouncyCastleProvider());

                File tmpFile=new File("Peer03.p12");
                KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
                SecureRandom random = new SecureRandom();
                keyPairGenerator.initialize(new RSAKeyGenParameterSpec(1024,
                                RSAKeyGenParameterSpec.F4), random);
                KeyPair keyPair = keyPairGenerator.generateKeyPair();

                X509V3CertificateGenerator certificateGenerator = new X509V3CertificateGenerator();
                certificateGenerator.setPublicKey(keyPair.getPublic());
                certificateGenerator.setSignatureAlgorithm("SHA1WithRSAEncryption");
                DateTime now = new DateTime();
                DateTime withinOneYear = now.plusYears(1);
                certificateGenerator.setNotBefore(now.toDate());
                certificateGenerator.setNotAfter(withinOneYear.toDate());
                certificateGenerator.setIssuerDN(new X509Principal( "C=BE, O=Frank Cornelis, OU=Software Development"));
                certificateGenerator.setSubjectDN(new X509Principal( "C=BE, O=Frank Cornelis, OU=Software Development"));
                certificateGenerator.setSerialNumber(new BigInteger("1234567890"));
                X509Certificate certificate = certificateGenerator.generate(keyPair.getPrivate());

                KeyStore keyStore = KeyStore.getInstance("pkcs12", BouncyCastleProvider.PROVIDER_NAME);
                char[] key = "password".toCharArray();
                keyStore.load(null, key);
                keyStore.setKeyEntry("alias", keyPair.getPrivate(), key, new Certificate[] { certificate });
                System.out.println( "Key: "+key+" private:"+keyPair.getPrivate()+"public:"+keyPair.getPublic());
                FileOutputStream keyStoreOut = new FileOutputStream(tmpFile);
                keyStore.store(keyStoreOut, key);
                keyStoreOut.close();
        }
        public static void main(String[] args) {
        try {
            GeneradorCertificados g = new GeneradorCertificados();
        } catch (Exception ex) {
            Logger.getLogger(GeneradorCertificados.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
} 